/*
 * Copyright (C) 2012-2013  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.remote.util;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

import nemos.util.PropertyHelper;

import org.osgi.framework.ServiceReference;
import org.osgi.service.remoteserviceadmin.EndpointDescription;
import org.osgi.service.remoteserviceadmin.EndpointListener;

/**
 * EplEndpointProvider : The {@link EndpointListener} version of the
 * {@link EndpointProvider}.
 * 
 * @author Romain DUBOIS
 */
public class EplEndpointProvider extends EndpointProvider<EndpointListener> {

    /**
     * Constructor
     */
    public EplEndpointProvider() {
        super();
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.remote.util.EndpointProvider#getListenerType()
     */
    @Override
    protected Class<EndpointListener> getListenerType() {
        return EndpointListener.class;
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.remote.util.EndpointProvider#notifyEndpoint(java.lang.Object,
     *      org.osgi.service.remoteserviceadmin.EndpointDescription,
     *      java.lang.String, boolean)
     */
    @Override
    protected void notifyEndpoint(final EndpointListener pListener,
            final EndpointDescription pEndpoint, final String pFilter, final boolean pAdded) {
        if (pAdded) {
            pListener.endpointAdded(pEndpoint, pFilter);
        } else {
            pListener.endpointRemoved(pEndpoint, pFilter);
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.remote.util.EndpointProvider#getScope(org.osgi.framework.ServiceReference)
     */
    @Override
    protected Collection<String> getScope(final ServiceReference<EndpointListener> pReference) {
        final Object lRawScope = pReference.getProperty(EndpointListener.ENDPOINT_LISTENER_SCOPE);
        final String[] lScope = PropertyHelper.getStringArray(lRawScope);
        if (lScope == null) {
            return Collections.emptyList();
        }
        return Arrays.asList(lScope);
    }

}
